package org.xxpay.manage.sys.ctrl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.MchConstant;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.domain.XxPayResponse;
import org.xxpay.core.common.util.DateUtil;
import org.xxpay.core.entity.MchAgentpayRecord;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.core.entity.SettRecord;
import org.xxpay.manage.common.ctrl.BaseController;

/**
 * layout页面 订单统计查询
 * */
@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/layout/newOrderCount")
public class LayoutOrderCountController extends BaseController {

    /** 查询统计数据 */
    @RequestMapping("/get")
    public ResponseEntity<?> get() {

        JSONObject param = getJsonParam();

        String reqDataStr = param.getString("reqDataStr"); //查询订单分类
        if(StringUtils.isEmpty(reqDataStr)){
            return ResponseEntity.ok(XxPayResponse.buildSuccess());
        }

        JSONObject result = new JSONObject();

        JSONArray array = JSONArray.parseArray(reqDataStr);
        for (Object o : array) {
            JSONObject jsonObject = (JSONObject)o;
            String queryOrderType = jsonObject.getString("name");
            String queryTimeStr = jsonObject.getString("queryTime");
            Integer count = 0;

            if("payOrderCount".equals(queryOrderType)){  //支付订单数量
                PayOrder payOrder = new PayOrder();
                payOrder.setStatus(PayConstant.PAY_STATUS_PAYING);
                payOrder.setPsVal("createdAtGeStr", queryTimeStr);
                count = commonService.payOrderService.count(payOrder);

            }else if("agentpayOrderCount".equals(queryOrderType)){ //代付订单数量
                MchAgentpayRecord mchAgentpayRecord = new MchAgentpayRecord();
                mchAgentpayRecord.setStatus(PayConstant.AGENTPAY_STATUS_WAIT_AUDIT);
                mchAgentpayRecord.setPsVal("createdAtGeStr", queryTimeStr);
                count = commonService.mchAgentpayService.count(mchAgentpayRecord);

            }else if("settOrderCount".equals(queryOrderType)){ //结算申请订单数量

                //查询结算订单数量
                SettRecord queryObjectBySett = new SettRecord();
                queryObjectBySett.setSettStatus(MchConstant.SETT_STATUS_AUDIT_ING);
                JSONObject othersCondition = new JSONObject();
                othersCondition.put("createTimeStart", DateUtil.str2date(queryTimeStr));
                count = commonService.settRecordService.count(queryObjectBySett, othersCondition);

            }

            result.put(queryOrderType, count);
        }

        return ResponseEntity.ok(XxPayResponse.buildSuccess(result));
    }

    /** 查询统计数据 配置项 */
    @RequestMapping("/getConfig")
    public ResponseEntity<?> getConfig() {
        return ResponseEntity.ok(XxPayResponse.buildSuccess(commonService.sysConfigService.getSysTipsConfig()));
    }

}
